package com.cgtong.android.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.cgtong.android.R;
import com.cgtong.android.base.BaseActivity;
import com.cgtong.android.bean.MessageBean;
import com.cgtong.android.http.ApiManager;
import com.cgtong.android.http.ConstantManager;
import com.umeng.analytics.MobclickAgent;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.ApiResult;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 消息详情
 */
public class MyMessageDetailActivity extends BaseActivity {

    @BindView(R.id.title_center_txt)
    TextView mTitleCenterTxt;
    @BindView(R.id.tv_content)
    TextView tv_content;
    @BindView(R.id.tv_time)
    TextView tv_time;
    @BindView(R.id.tv_title)
    TextView tv_title;

    @OnClick(R.id.title_left_txt)
    public void onViewClicked(View view) {
        finish();
    }

    private MessageBean msg;

    public static void start2Activity(Context context, MessageBean msg) {
        Intent intent = new Intent(context, MyMessageDetailActivity.class);
        intent.putExtra("msg", msg);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_message_detail;
    }

    @Override
    public void initData() {
        mTitleCenterTxt.setText(getString(R.string.title_message));
        msg = (MessageBean) getIntent().getSerializableExtra("msg");
        tv_title.setText(msg.getTitle());
        tv_time.setText(msg.getPushTime());
        tv_content.setText(msg.getContent());
        if (ConstantManager.ReadStatus.UNRED == msg.getReadStatus()) {
            updateRead();
        }
    }

    private void updateRead() {
        EasyHttp.post(ApiManager.URL_UPDATEUNREAD)
                .params("id", msg.getId() + "")
                .execute(new SimpleCallBack<List<ApiResult>>() {
                    @Override
                    public void onError(ApiException e) {
                        ToastUtils.showShort(e.getMessage());
                    }

                    @Override
                    public void onSuccess(List<ApiResult> result) {
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("消息详情");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("消息详情");
        MobclickAgent.onPause(this);
    }
}